public class Solution4 {
    public int trap(int[] height) {
        int left = 0,right = height.length - 1;
        int l_max = 0,r_max = 0;
        int theWater = 0;
        while(left < right){
            if(height[left] > l_max){
                l_max = height[left];
            }
            if (height[right] > r_max){
                r_max = height[right];
            }

            if(l_max < r_max){
                theWater += l_max - height[left];
                left++;
            }else{
                theWater += r_max - height[right];
                right--;
            }
        }
        return theWater;
    }
}
